import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../core/theme/app_colors.dart';
import '../../core/theme/app_text_styles.dart';
import '../../core/constants/app_constants.dart';
import '../../core/constants/emotion_constants.dart';
import '../../core/utils/app_utils.dart';

/// 情绪记录卡片组件 - 用于显示情绪记录的摘要信息
/// Emotion Record Card Widget - For displaying emotion record summary
class EmotionRecordCard extends StatelessWidget {
  /// 记录数据 / Record data
  final dynamic record;
  
  /// 点击回调 / Tap callback
  final VoidCallback? onTap;
  
  /// 是否显示日期 / Whether to show date
  final bool showDate;
  
  /// 是否显示完整内容 / Whether to show full content
  final bool showFullContent;
  
  /// 最大内容行数 / Maximum content lines
  final int maxContentLines;

  const EmotionRecordCard({
    super.key,
    required this.record,
    this.onTap,
    this.showDate = false,
    this.showFullContent = false,
    this.maxContentLines = 3,
  });

  @override
  Widget build(BuildContext context) {
    final emotionColor = EmotionConstants.getEmotionColor(record.emotionType.value);
    final emotionEmoji = EmotionConstants.getEmotionEmoji(record.emotionType.value);
    final emotionLabel = EmotionConstants.getEmotionLabel(record.emotionType.value);
    final intensityLabel = EmotionConstants.getIntensityLabel(record.intensity);

    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.all(16.w),
        decoration: BoxDecoration(
          color: AppColors.surface,
          borderRadius: BorderRadius.circular(12.r),
          boxShadow: AppConstants.shadowSoft,
          border: Border.all(
            color: emotionColor.withOpacity(0.2),
            width: 1.w,
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 头部信息 / Header information
            _buildHeader(emotionColor, emotionEmoji, emotionLabel, intensityLabel),
            
            // 日记内容 / Diary content
            if (record.diaryContent.isNotEmpty) ...[
              SizedBox(height: 16.h),
              _buildContent(),
            ],
            
            // 媒体和标签信息 / Media and tags information
            SizedBox(height: 16.h),
            _buildFooter(),
          ],
        ),
      ),
    );
  }

  /// 构建头部信息
  /// Build header information
  Widget _buildHeader(
    Color emotionColor,
    String emotionEmoji,
    String emotionLabel,
    String intensityLabel,
  ) {
    return Row(
      children: [
        // 情绪图标和标签 / Emotion icon and label
        Container(
          padding: const EdgeInsets.all(AppConstants.paddingSmall),
          decoration: BoxDecoration(
            color: emotionColor.withOpacity(0.1),
            borderRadius: BorderRadius.circular(AppConstants.radiusSmall),
            border: Border.all(color: emotionColor.withOpacity(0.3)),
          ),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                emotionEmoji,
                style: const TextStyle(fontSize: 20),
              ),
              const SizedBox(width: AppConstants.spacingSmall),
              Text(
                emotionLabel,
                style: AppTextStyles.bodyMedium.copyWith(
                  color: emotionColor,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ],
          ),
        ),
        
        const Spacer(),
        
        // 时间信息 / Time information
        Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            if (showDate) ...[
              Text(
                AppUtils.formatDate(record.createdAt),
                style: AppTextStyles.bodySmall.copyWith(
                  color: AppColors.textSecondary,
                ),
              ),
              const SizedBox(height: 2),
            ],
            Text(
              AppUtils.formatTime(record.createdAt),
              style: AppTextStyles.bodySmall.copyWith(
                color: AppColors.textSecondary,
              ),
            ),
          ],
        ),
      ],
    );
  }

  /// 构建内容区域
  /// Build content area
  Widget _buildContent() {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(AppConstants.paddingMedium),
      decoration: BoxDecoration(
        color: AppColors.background,
        borderRadius: BorderRadius.circular(AppConstants.radiusSmall),
        border: Border.all(color: AppColors.border),
      ),
      child: Text(
        record.diaryContent,
        style: AppTextStyles.bodyMedium.copyWith(
          height: 1.5,
        ),
        maxLines: showFullContent ? null : maxContentLines,
        overflow: showFullContent ? null : TextOverflow.ellipsis,
      ),
    );
  }

  /// 构建底部信息
  /// Build footer information
  Widget _buildFooter() {
    final hasImages = record.imagePaths?.isNotEmpty ?? false;
    final hasAudio = record.audioPath?.isNotEmpty ?? false;
    final hasTags = record.tags?.isNotEmpty ?? false;
    final emotionColor = EmotionConstants.getEmotionColor(record.emotionType.value);

    return Row(
      children: [
        // 强度指示器 / Intensity indicator
        _buildIntensityIndicator(emotionColor),
        
        const Spacer(),
        
        // 媒体和标签图标 / Media and tags icons
        Row(
          children: [
            // 图片图标 / Image icon
            if (hasImages) ...[
              _buildInfoIcon(
                icon: Icons.image,
                count: record.imagePaths?.length ?? 0,
                color: AppColors.info,
              ),
              const SizedBox(width: AppConstants.spacingSmall),
            ],
            
            // 录音图标 / Audio icon
            if (hasAudio) ...[
              _buildInfoIcon(
                icon: Icons.audiotrack,
                count: 1,
                color: AppColors.warning,
              ),
              const SizedBox(width: AppConstants.spacingSmall),
            ],
            
            // 标签图标 / Tags icon
            if (hasTags) ...[
              _buildInfoIcon(
                icon: Icons.local_offer,
                count: record.tags?.length ?? 0,
                color: AppColors.success,
              ),
            ],
            
            // 如果没有任何附加内容，显示字数 / Show word count if no additional content
            if (!hasImages && !hasAudio && !hasTags && record.diaryContent.isNotEmpty) ...[
              _buildInfoIcon(
                icon: Icons.text_fields,
                count: record.diaryContent.length,
                color: AppColors.textSecondary,
                showAsText: true,
              ),
            ],
          ],
        ),
      ],
    );
  }

  /// 构建强度指示器
  /// Build intensity indicator
  Widget _buildIntensityIndicator(Color emotionColor) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '强度 ${record.intensity}/10',
          style: AppTextStyles.bodySmall.copyWith(
            color: AppColors.textSecondary,
          ),
        ),
        const SizedBox(height: 4),
        Container(
          width: 60,
          height: 4,
          decoration: BoxDecoration(
            color: AppColors.border,
            borderRadius: BorderRadius.circular(2),
          ),
          child: FractionallySizedBox(
            alignment: Alignment.centerLeft,
            widthFactor: record.intensity / 10.0,
            child: Container(
              decoration: BoxDecoration(
                color: emotionColor,
                borderRadius: BorderRadius.circular(2),
              ),
            ),
          ),
        ),
      ],
    );
  }

  /// 构建信息图标
  /// Build info icon
  Widget _buildInfoIcon({
    required IconData icon,
    required int count,
    required Color color,
    bool showAsText = false,
  }) {
    return Container(
      padding: const EdgeInsets.symmetric(
        horizontal: AppConstants.paddingSmall,
        vertical: 2,
      ),
      decoration: BoxDecoration(
        color: color.withOpacity(0.1),
        borderRadius: BorderRadius.circular(AppConstants.radiusSmall),
        border: Border.all(color: color.withOpacity(0.3)),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            icon,
            size: 14,
            color: color,
          ),
          const SizedBox(width: 2),
          Text(
            showAsText ? '$count字' : '$count',
            style: AppTextStyles.bodySmall.copyWith(
              color: color,
              fontSize: 11,
            ),
          ),
        ],
      ),
    );
  }
}

/// 简化版情绪记录卡片 - 用于首页等场景
/// Simplified Emotion Record Card - For home page and other scenarios
class SimpleEmotionRecordCard extends StatelessWidget {
  /// 记录数据 / Record data
  final dynamic record;
  
  /// 点击回调 / Tap callback
  final VoidCallback? onTap;

  const SimpleEmotionRecordCard({
    super.key,
    required this.record,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    final emotionColor = EmotionConstants.getEmotionColor(record.emotionType);
    final emotionEmoji = EmotionConstants.getEmotionEmoji(record.emotionType);
    final emotionLabel = EmotionConstants.getEmotionLabel(record.emotionType);

    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.all(AppConstants.paddingMedium),
        decoration: BoxDecoration(
          color: AppColors.surface,
          borderRadius: BorderRadius.circular(AppConstants.radiusMedium),
          boxShadow: AppConstants.shadowSoft,
          border: Border.all(
            color: emotionColor.withOpacity(0.2),
            width: 1,
          ),
        ),
        child: Row(
          children: [
            // 情绪图标 / Emotion icon
            Container(
              padding: const EdgeInsets.all(AppConstants.paddingSmall),
              decoration: BoxDecoration(
                color: emotionColor.withOpacity(0.1),
                borderRadius: BorderRadius.circular(AppConstants.radiusSmall),
              ),
              child: Text(
                emotionEmoji,
                style: const TextStyle(fontSize: 24),
              ),
            ),
            
            const SizedBox(width: AppConstants.spacingMedium),
            
            // 情绪信息 / Emotion information
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    emotionLabel,
                    style: AppTextStyles.titleSmall.copyWith(
                      color: emotionColor,
                    ),
                  ),
                  const SizedBox(height: 2),
                  Text(
                    '强度 ${record.intensity}/10',
                    style: AppTextStyles.bodySmall.copyWith(
                      color: AppColors.textSecondary,
                    ),
                  ),
                  if (record.content.isNotEmpty) ...[
                    const SizedBox(height: 4),
                    Text(
                      record.content,
                      style: AppTextStyles.bodySmall.copyWith(
                        color: AppColors.textSecondary,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ],
                ],
              ),
            ),
            
            // 时间信息 / Time information
            Text(
              AppUtils.formatTime(record.createdAt),
              style: AppTextStyles.bodySmall.copyWith(
                color: AppColors.textSecondary,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

/// 情绪记录网格卡片 - 用于网格布局
/// Emotion Record Grid Card - For grid layout
class GridEmotionRecordCard extends StatelessWidget {
  /// 记录数据 / Record data
  final dynamic record;
  
  /// 点击回调 / Tap callback
  final VoidCallback? onTap;

  const GridEmotionRecordCard({
    super.key,
    required this.record,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    final emotionColor = EmotionConstants.getEmotionColor(record.emotionType);
    final emotionEmoji = EmotionConstants.getEmotionEmoji(record.emotionType);
    final emotionLabel = EmotionConstants.getEmotionLabel(record.emotionType);

    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.all(AppConstants.paddingMedium),
        decoration: BoxDecoration(
          color: AppColors.surface,
          borderRadius: BorderRadius.circular(AppConstants.radiusMedium),
          boxShadow: AppConstants.shadowSoft,
          border: Border.all(
            color: emotionColor.withOpacity(0.2),
            width: 1,
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 情绪图标和时间 / Emotion icon and time
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  emotionEmoji,
                  style: const TextStyle(fontSize: 28),
                ),
                Text(
                  AppUtils.formatTime(record.createdAt),
                  style: AppTextStyles.bodySmall.copyWith(
                    color: AppColors.textSecondary,
                  ),
                ),
              ],
            ),
            
            const SizedBox(height: AppConstants.spacingSmall),
            
            // 情绪标签 / Emotion label
            Text(
              emotionLabel,
              style: AppTextStyles.titleSmall.copyWith(
                color: emotionColor,
              ),
            ),
            
            const SizedBox(height: AppConstants.spacingSmall),
            
            // 强度指示器 / Intensity indicator
            Container(
              width: double.infinity,
              height: 4,
              decoration: BoxDecoration(
                color: AppColors.border,
                borderRadius: BorderRadius.circular(2),
              ),
              child: FractionallySizedBox(
                alignment: Alignment.centerLeft,
                widthFactor: record.intensity / 10.0,
                child: Container(
                  decoration: BoxDecoration(
                    color: emotionColor,
                    borderRadius: BorderRadius.circular(2),
                  ),
                ),
              ),
            ),
            
            const SizedBox(height: AppConstants.spacingSmall),
            
            // 内容预览 / Content preview
            if (record.content.isNotEmpty)
              Expanded(
                child: Text(
                  record.content,
                  style: AppTextStyles.bodySmall.copyWith(
                    color: AppColors.textSecondary,
                    height: 1.4,
                  ),
                  maxLines: 3,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
          ],
        ),
      ),
    );
  }
}